angular.module('Controller')
.controller('QgyApartmentRoomEditCtrl', ($scope, urls,authorityManage, $localStorage, NgTableParams, commonUtil, cryptUtil, wfAlert, commonConstant, $http, $uibModalInstance, items)->
    type = items.type
    $scope.type = items.type
    $scope.data = items.data
    console.log($scope.data)
    $scope.data.lightDark=true if $scope.data.lightDark==undefined
    $scope.info = info = items.info
    $scope.modalTitle = if type == 'new' then '新建房间' else '编辑房间'
    $scope.editPriceAu=authorityManage.check('modify_house_price')
    $scope.modalEnableList = commonConstant.enabledList()
    $scope.houseStyleList = commonConstant.houseStyleList()
    $scope.orientationsList = commonConstant.orientationsList()
    $scope.lightDarkList=commonConstant.lightDarkList()

    $scope.modalPayTypeList = commonConstant.payWayList()

    $scope.roomStatusList = commonConstant.roomActiveStatusList()
    $scope.layoutList = []
    loadData = ()->
        $scope.layoutList = []
        $http.get('/layout', params: {page: 0, size: 999, apartmentId: info?.apartmentId})
        .success((resp)->
            if resp.code == 0
                $scope.layoutList = resp.message.content
                if $scope.data.layoutId == undefined and $scope.layoutList.length > 0 and type=='new'
                    $scope.data.layoutId = $scope.layoutList[0].id
        )
        $scope.configureList=[]
        $http.get('/configureType', params: {page: 0, size: 999, apartmentId: info?.apartmentId})
        .success((resp)->
            if resp.code == 0
                $scope.configureList = resp.message.content
                if $scope.data.configureId == undefined and $scope.configureList.length > 0 and type=='new'
                    $scope.data.configureId = $scope.configureList[0].id
        )
    loadData()
    _.extend $scope,
        newData: ()->
            reqData = {
                "apartmentId": $scope.data.apartmentId
                "buildingId": $scope.data.buildingId
                "floor": $scope.data.floor
                "layoutId": $scope.data.layoutId
                "configureId": $scope.data.configureId
                "style": $scope.data.style || 1
                "houseNo": $scope.data.houseNo
                "area": $scope.data.area
                "salePrice": $scope.data.salePrice
                "depositPrice": $scope.data.salePrice
                "payType": $scope.data.payType
                "depositRemark": $scope.data.depositRemark
                "orientations": $scope.data.orientations
                "lightDark":$scope.data.lightDark
                "status": $scope.data.status
                "enabled": $scope.data.enabled
                "width": parseInt($scope.data.width)
                "height": parseInt($scope.data.height)
                "left": parseInt($scope.data.left)
                "top": parseInt($scope.data.top)
                #add
                "doorCard":$scope.data.doorCard
                "waterUuid":$scope.data.waterUuid
                "powerUuid":$scope.data.powerUuid
            }
            $http.post('/house', reqData)
            .success((resp)->
                if resp.code == 0
                    wfAlert.toastInfo($scope.modalTitle + '成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData: ()->
            reqData = {
#                "apartmentId" : $scope.info.apartmentId
#                "buildingId" : $scope.info.buildingId
#                "floor" : $scope.info.floor
                "layoutId": $scope.data.layoutId
                "configureId": $scope.data.configureId
                "style": $scope.data.style
                "houseNo": $scope.data.houseNo
                "area": $scope.data.area
                "salePrice": $scope.data.salePrice
                "depositPrice": $scope.data.salePrice
                "payType": $scope.data.payType
                "depositRemark": $scope.data.depositRemark
                "orientations": $scope.data.orientations
                "lightDark":$scope.data.lightDark
                "status": $scope.data.status
                "enabled": $scope.data.enabled
                "width": parseInt($scope.data.width)
                "height": parseInt($scope.data.height)
                "left": parseInt($scope.data.left)
                "top": parseInt($scope.data.top)
                #add
                "doorCard":$scope.data.doorCard
                "waterUuid":$scope.data.waterUuid
                "powerUuid":$scope.data.powerUuid
            }
            $http.put("/house/#{$scope.data.id}", reqData)
            .success((resp)->
                if resp.code == 0
                    wfAlert.toastInfo($scope.modalTitle + '成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type == 'new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel = ()->
        $uibModalInstance.dismiss('cancel')
)